/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.smart.common.scheduler.service;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.smart.common.enums.ReportTypeEnum;
import com.smart.common.enums.SmsTemplateEnum;
import com.smart.mapper.ProjectMapper;
import com.smart.mapper.ProjectWeeklyTaskMapper;
import com.smart.model.domain.ProjectDO;
import com.smart.model.domain.ProjectWeeklyTaskDO;
import com.smart.utils.SmsUtil;
import com.smart.utils.WeekUtil;
import com.sun.xml.bind.marshaller.Messages;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhanghong
 * @version SmsScheduleService.java, v 0.1 2023/9/25 14:06 下午 SmsScheduleService Exp $$
 */
@Slf4j
@Component
public class WeeklyTaskScheduleSmsService {

    @Autowired
    private ProjectWeeklyTaskMapper projectWeeklyTaskMapper;
    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 触发条件，每周一上午十点，本周有周报但是还未提报的短信提醒
     * @return
     */
    public boolean weeklyTaskSunday() {
        Date date = new Date();
        int weekNum = WeekUtil.getWeekNum();
        int yearNum = DateUtil.year(date);
        ProjectWeeklyTaskDO weeklyTaskDO = projectWeeklyTaskMapper.queryByYearAndWeek(yearNum, weekNum);
        if (weeklyTaskDO == null) {
            log.info("当周不存在周报任务，无需提醒");
            return true;
        }

        List<ProjectDO> projects = projectMapper.listUnReportByReportIdAndItemType(weeklyTaskDO.getId(), ReportTypeEnum.WEEKLY.name());
        if (CollectionUtils.isEmpty(projects)) {
            log.info("不存在对应的项目信息");
            return true;
        }

        DateTime dateTime = DateUtil.beginOfWeek(date);
        String dateStr = DateUtil.offsetDay(dateTime, 3).toDateStr();

        LinkedHashMap<String, String> messages = new LinkedHashMap<>();
        messages.put("time", String.format("%s 18:00", dateStr));
        for (ProjectDO projectDO : projects) {
            //SmsUtil.sendMessage(Lists.newArrayList(projectDO.getShigongContactPhone()), SmsTemplateEnum.SMS_463196284, messages);
        }
        return true;
    }

    /**
     * 触发条件，每周四下午2点，本周有周报但是还未提报的短信提醒
     * @return
     */
    public boolean weeklyTaskThursday() {
        Date date = new Date();
        int weekNum = WeekUtil.getWeekNum();
        int yearNum = DateUtil.year(date);
        ProjectWeeklyTaskDO weeklyTaskDO = projectWeeklyTaskMapper.queryByYearAndWeek(yearNum, weekNum);
        if (weeklyTaskDO == null) {
            log.info("当周不存在周报任务，无需提醒");
            return true;
        }

        List<ProjectDO> projects = projectMapper.listUnReportByReportIdAndItemType(weeklyTaskDO.getId(), ReportTypeEnum.WEEKLY.name());
        if (CollectionUtils.isEmpty(projects)) {
            log.info("不存在对应的项目信息");
            return true;
        }

        String dateStr = DateUtil.format(date, DatePattern.NORM_DATE_PATTERN);

        LinkedHashMap<String, String> messages = new LinkedHashMap<>();
        messages.put("time", String.format("%s 18:00", dateStr));

        for (ProjectDO projectDO : projects) {
            SmsUtil.sendMessage(Lists.newArrayList(projectDO.getShigongContactPhone(), projectDO.getShigongHeadPhone()), SmsTemplateEnum.SMS_463226022, messages);
        }
        return true;
    }
}
